#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：大路网网格化仿真
@File ：gj_kafka.py
@IDE ：PyCharm
@Author ：xinyingjie
@Date ：2025/2/19 10:28
'''
import json
import os
import threading
from pathlib import Path


def write_json():
    folder_path = 'D:\GJ\项目\事故检测\json2'
    result = {}
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if 20240124 <= int(str(file)[4:12]) <= 20240131:
                file_path = os.path.join(root, file)  # 获取完整文件路径
                with open(file_path, 'r', encoding='utf-8') as f:
                    for line in f:
                        data_json = json.loads(line.strip())
                        for index, data_one in enumerate(data_json):
                            if index not in result.keys():
                                # print(data_one)
                                data_one['gantry_id'] = data_one['gantry_id'] + "_" + str(file)[4:12]
                                result[index] = [data_one]
                            else:
                                data_one['gantry_id'] = data_one['gantry_id'] + "_" + str(file)[4:12]
                                result[index].append(data_one)
    with open('result_20240124.json', 'a', encoding='utf-8') as file_w:
        for k, v in result.items():
            file_w.write(str(v).replace("'", '"') + "\n")


def write_json_1():
    folder_path = 'D:\GJ\项目\事故检测\json2'
    result = {}
    for root, dirs, files in os.walk(folder_path):

        for file in files:
            # print(int(str(file)[4:12]))
            if 20250201 <= int(str(file)[4:12]) <= 20250207:
                file_path = os.path.join(root, file)  # 获取完整文件路径
                with open(file_path, 'r', encoding='utf-8') as f:
                    index = 0
                    for line in f:
                        index += 1
                        data_json = json.loads(line.strip())
                        data_json['gantry_id'] = data_json['gantry_id'] + "_" + str(file)[4:12]
                        if index not in result.keys():
                            result[index] = [data_json]
                        else:
                            result[index].append(data_json)
    with open(r'.\result_20240201.json', 'a', encoding='utf-8') as file_w:
        for k, v in result.items():
            file_w.write(str(v).replace("'", '"') + "\n")



# write_json()
from kafka import KafkaConsumer, KafkaProducer
import time
import json
import queue


def send_data():
    # 需要发动到的topic
    KAFKA_HOST = "10.100.12.58:9093"
    PRE_DATA_TOPIC = "KAKOU_DATA_TOPIC"
    # 发送数据
    now_frame_data = {}
    producer = KafkaProducer(bootstrap_servers=KAFKA_HOST, key_serializer=str.encode,
                             value_serializer=lambda x: json.dumps(x).encode('utf-8'))

    with open('result2.json', 'r', encoding='utf-8') as file_w:
        while True:
            line = file_w.readline().strip()
            if line:
                data = json.loads(line)
                producer.send(PRE_DATA_TOPIC, value=data, key="key")
                time.sleep(0.2)
            else:
                break


# # 需要发动到的topic
# KAFKA_HOST = "10.100.12.58:9093"
# PRE_DATA_TOPIC = "KAKOU_DATA_TOPIC"
# # 发送数据
# now_frame_data = {}
# producer = KafkaProducer(bootstrap_servers=KAFKA_HOST, key_serializer=str.encode,
#                          value_serializer=lambda x: json.dumps(x).encode('utf-8'))
#

def send_data_one(file_path, topic):
    with open(file_path, 'r', encoding='utf-8') as file_w:
        while True:
            line = file_w.readline().strip()
            if line:
                data = json.loads(line)
                producer.send(topic, value=data, key="key")
                time.sleep(0.1)
            else:
                break


def send_data_all():
    folder_path = r'G:\xinyingjie\proj\大路网网格化仿真\苏研事件检测\gj_data'

    # threading.Thread(target=send_data_one, args=(
    # r'G:\xinyingjie\proj\大路网网格化仿真\苏研事件检测\gj_data\result_20240101.json',
    # PRE_DATA_TOPIC + "_1")).start()
    threading.Thread(target=send_data_one, args=(
        r'G:\xinyingjie\proj\大路网网格化仿真\苏研事件检测\gj_data\result_202306704.json',
        PRE_DATA_TOPIC)).start()


if __name__ == '__main__':
    # write_json_1()
    # send_data_all()

    print("1")